package com.cmccgz.udacs_landlord_webapp.data.remote;

import android.support.annotation.NonNull;

import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.bean.AccreditCardBean;
import com.cmccgz.udacs_landlord_webapp.bean.BuildingCountBean;
import com.cmccgz.udacs_landlord_webapp.bean.HouseListDataBean;
import com.cmccgz.udacs_landlord_webapp.bean.HousePeopleBean;
import com.cmccgz.udacs_landlord_webapp.bean.MobileBean;
import com.cmccgz.udacs_landlord_webapp.bean.ResultBean;
import com.cmccgz.udacs_landlord_webapp.data.BaseCallback;
import com.cmccgz.udacs_landlord_webapp.data.HouseManagerDataSource;

import java.util.List;

import rx.Subscriber;

/**
 * @author: Huang
 * @Descripion:
 * @date 2017/6/22 15:05
 */

public class HouseManagerRemoteDataSource implements HouseManagerDataSource {

    private static HouseManagerRemoteDataSource INSTANCE;

    public static HouseManagerRemoteDataSource getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new HouseManagerRemoteDataSource();
        }
        return INSTANCE;
    }

    // Prevent direct instantiation.
    private HouseManagerRemoteDataSource() {
    }

    @Override
    public void getFunctionList(@NonNull FunctionListCallback callback) {

    }

    @Override
    public void getHouseList(@NonNull String searchType, @NonNull String keyword, @NonNull String oCond, @NonNull final int page, @NonNull final HouseListCallback callback) {
        ApiService.getHouseList(searchType, keyword, oCond, page)
                .subscribe(new Subscriber<ResultBean<List<HouseListDataBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.onLoadedFailed("网络异常！");
                    }

                    @Override
                    public void onNext(ResultBean<List<HouseListDataBean>> listResultBean) {
                        String rscode = listResultBean.rsmsg.rscode;
                        if ("0".equals(rscode)) {
                            callback.onHouseListLoaded(listResultBean.resp);
                        } else {
                            callback.onLoadedFailed(listResultBean.rsmsg.rsdesc);
                        }
                    }
                });
    }

    @Override
    public void getHouseDetail(@NonNull String houseId, @NonNull final HouseDetailCallback callback) {
        ApiService.getHouseDetail(houseId)
                .subscribe(new Subscriber<ResultBean<List<HousePeopleBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.onLoadedFailed("网络异常！");
                    }

                    @Override
                    public void onNext(ResultBean<List<HousePeopleBean>> listResultBean) {
                        if ("0".equals(listResultBean.rsmsg.rscode)) {
                            callback.onHouseDetailLoaded(listResultBean.resp);
                        } else {
                            callback.onLoadedFailed(listResultBean.rsmsg.rsdesc);
                        }
                    }
                });
    }

    @Override
    public void getBuildingCount(@NonNull String buildingId, @NonNull final BuildingCountCallback callback) {
        ApiService.buildingCount(buildingId)
                .subscribe(new Subscriber<ResultBean<List<BuildingCountBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.onLoadedFailed("网络异常！");
                    }

                    @Override
                    public void onNext(ResultBean<List<BuildingCountBean>> listResultBean) {
                        if ("0".equals(listResultBean.rsmsg.rscode)) {
                            callback.onBuildingCountLoaded(listResultBean.resp);
                        } else {
                            callback.onLoadedFailed(listResultBean.rsmsg.rsdesc);
                        }
                    }
                });
    }

    @Override
    public void checkout(@NonNull List<MobileBean> bean, @NonNull String houseId, @NonNull final BaseCallback callback) {
        ApiService.checkOut(bean, houseId)
                .subscribe(new Subscriber<ResultBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.onFailed("网络异常！");
                    }

                    @Override
                    public void onNext(ResultBean resultBean) {
                        if ("0".equals(resultBean.rsmsg.rscode)) {
                            callback.onSuccess();
                        } else {
                            callback.onFailed(resultBean.rsmsg.rsdesc);
                        }
                    }
                });
    }

    @Override
    public void houseChange(@NonNull List<MobileBean> bean, @NonNull String oldHouseId, @NonNull String newHouseId, @NonNull final BaseCallback callback) {
        ApiService.houseChange(bean, oldHouseId, newHouseId)
                .subscribe(new Subscriber<ResultBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.onFailed("网络异常！");
                    }

                    @Override
                    public void onNext(ResultBean resultBean) {
                        if ("0".equals(resultBean.rsmsg.rscode)) {
                            callback.onSuccess();
                        } else {
                            callback.onFailed(resultBean.rsmsg.rsdesc);
                        }
                    }
                });
    }

    @Override
    public void accreditCard(@NonNull AccreditCardBean bean, @NonNull final BaseCallback callback) {
        ApiService.accreditCard(bean)
                .subscribe(new Subscriber<ResultBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.onFailed("网络异常！");
                    }

                    @Override
                    public void onNext(ResultBean resultBean) {
                        if ("0".equals(resultBean.rsmsg.rscode)) {
                            callback.onSuccess();
                        } else {
                            callback.onFailed(resultBean.rsmsg.rsdesc);
                        }
                    }
                });
    }
}
